<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="采购单未付款明细"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <purchaseWjs ref="purchaseWjs"></purchaseWjs>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>

<script>
import VolBox from "@/components/basic/VolBox.vue";
import purchaseWjs from "@/views/purchaseinfo/q_purchase_wjs/q_purchase_wjs.vue";
export default {
  components: {
    "vol-box": VolBox,
    purchaseWjs,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
    },
    open(WHSE_no) {
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.purchaseWjs) {
          clearInterval(setIntervalTiem);
          console.log("this.searchFormOptions",this.searchFormOptions)
          this.$refs.purchaseWjs.searchFormOptions.forEach(item=>{
            item.forEach(x=>{
              if(x.field=="机构"){
                x.disabled=true
              }
            })
          })
          this.$refs.purchaseWjs.searchFormFields.机构=WHSE_no
          this.$refs.purchaseWjs.$refs.grid.search();
        }
      }, 100);
      this.model = true;
    },
    async detailOnSelect() {
      try{
      let rowObj = this.$refs.purchaseWjs.$refs.grid.getSelectRows();
      rowObj.forEach(item=>{
        item.Voucher_sub=item.单号
      })
      if (!rowObj.length) return this.base.ElMessageFn("请至少选择一条采购单");
      this.$emit("parentCall", ($parent) => {
        let parentList = $parent.$refs.detail.rowData;
        let set = new Set(parentList.map((item) => item.Voucher_sub)); //得到由明细表goods_sub组成的数组
        let commonObjects = rowObj.filter((item) => set.has(item.Voucher_sub)); //得到明细表中已包含的选中行
        let goodsNamesSet = new Set(
          commonObjects.map((item) => item.Voucher_sub)
        ); //相同商品的编码
        if (commonObjects.length) {
          rowObj = rowObj.filter((item) => !goodsNamesSet.has(item.Voucher_sub)); //在选中数据中剔除明细表已包含的行数据
          let str = "";
          commonObjects.forEach((r) => {
            if (str == "") str += r.单号;
            else str = str + "," + r.单号;
          });
          this.$message.warning(`采购单${str}已存在此付款单`);
        }
        console.log("rowObj",rowObj)
        let arr = rowObj.map((item) => {
          return {
            Voucher_sub: item.单号,
            Unpaidamount: item.未结算金额,//未付款金额
            adjust:0,//设置默认调整金额
            // adjustRemark: item.Total_price,//调整原因
            Supplier: item.供应商,
            PaymentType: item.结款方式,
          };
        });
        $parent.$refs.detail.rowData.unshift(...arr);
        if(arr.length&&this.$route.path == "/q_purchase_payment"){
          $parent.detailFn()
        }
        this.onModelClose();
        return (this.model = false);
      });
     }catch (e){
      console.log(e)
     }
    },
  },
};
</script>

<style>
</style>